<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        
        <title>
                        Defs and Blocks
                     &mdash; Mako 0.4.1 Documentation</title>
        
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    <link rel="stylesheet" href="_static/docs.css" type="text/css" />

    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
          URL_ROOT:    '#',
          VERSION:     '0.4.1',
          COLLAPSE_MODINDEX: false,
          FILE_SUFFIX: '.html'
      };
    </script>
        <script type="text/javascript" src="_static/jquery.js"></script>
        <script type="text/javascript" src="_static/underscore.js"></script>
        <script type="text/javascript" src="_static/doctools.js"></script>
    <script type="text/javascript" src="_static/init.js"></script>
    <link rel="index" title="Index" href="genindex.html" />
    <link rel="search" title="Search" href="search.html" />
    <link rel="top" title="Mako 0.4.1 Documentation" href="index.html" />
        <link rel="next" title="The Mako Runtime Environment" href="runtime.html" />
        <link rel="prev" title="Syntax" href="syntax.html" />

    
    

    </head>
    <body>
        



        <h1>Mako 0.4.1 Documentation</h1>

        <div id="search">
        Search:
        <form class="search" action="search.html" method="get">
          <input type="text" name="q" size="18" /> <input type="submit" value="Search" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
        </div>

        <div class="versionheader">
            Version: <span class="versionnum">0.4.1</span> Last Updated: 04/06/2011 19:09:35
        </div>
        <div class="clearboth"></div>

        <div class="topnav">
            <div id="pagecontrol">
                <a href="genindex.html">Index</a>
 
                <div class="sourcelink">(<a href="_sources/defs.txt">view source)</div>
            </div>
 
            <div class="navbanner">
                <a class="totoc" href="index.html">Table of Contents</a>
                » 
                        Defs and Blocks
                     

                
<div class="prevnext">
        Previous:
        <a href="syntax.html" title="previous chapter">Syntax</a>
        Next:
        <a href="runtime.html" title="next chapter">The Mako Runtime Environment</a>
</div>

                <h2>
                    
                        Defs and Blocks
                    
                </h2>
            </div>
                <ul>
<li><a class="reference internal" href="#">Defs and Blocks</a><ul>
<li><a class="reference internal" href="#using-defs">Using Defs</a><ul>
<li><a class="reference internal" href="#calling-defs-from-other-files">Calling defs from Other Files</a></li>
<li><a class="reference internal" href="#calling-defs-programmatically">Calling defs programmatically</a></li>
<li><a class="reference internal" href="#defs-within-defs">Defs within Defs</a></li>
<li><a class="reference internal" href="#calling-a-def-with-embedded-content-and-or-other-defs">Calling a def with embedded content and/or other defs</a></li>
</ul>
</li>
<li><a class="reference internal" href="#using-blocks">Using Blocks</a><ul>
<li><a class="reference internal" href="#using-named-blocks">Using Named Blocks</a></li>
<li><a class="reference internal" href="#using-page-arguments-in-named-blocks">Using page arguments in named blocks</a></li>
</ul>
</li>
</ul>
</li>
</ul>

            <div class="clearboth"></div>
        </div>
 
        <div class="document">
            <div class="body">
                
<div class="section" id="defs-and-blocks">
<span id="defs-toplevel"></span><h1>Defs and Blocks<a class="headerlink" href="#defs-and-blocks" title="Permalink to this headline">¶</a></h1>
<p><tt class="docutils literal"><span class="pre">&lt;%def&gt;</span></tt> and <tt class="docutils literal"><span class="pre">&lt;%block&gt;</span></tt> are two tags that both demarcate any block of text
and/or code.   They both exist within generated Python as a callable function,
i.e., a Python <tt class="docutils literal"><span class="pre">def</span></tt>.   They differ in their scope and calling semantics.
Whereas <tt class="docutils literal"><span class="pre">&lt;%def&gt;</span></tt> provides a construct that is very much like a named Python
<tt class="docutils literal"><span class="pre">def</span></tt>, the <tt class="docutils literal"><span class="pre">&lt;%block&gt;</span></tt> is more layout oriented.</p>
<div class="section" id="using-defs">
<h2>Using Defs<a class="headerlink" href="#using-defs" title="Permalink to this headline">¶</a></h2>
<p>The <tt class="docutils literal"><span class="pre">&lt;%def&gt;</span></tt> tag requires a <tt class="docutils literal"><span class="pre">name</span></tt> attribute, where the <tt class="docutils literal"><span class="pre">name</span></tt> references
a Python function signature:</p>
<div class="highlight-mako"><div class="highlight"><pre><span class="cp">&lt;%</span><span class="nb">def</span> <span class="na">name=</span><span class="s">&quot;hello()&quot;</span><span class="cp">&gt;</span><span class="x"></span>
<span class="x">    hello world</span>
<span class="cp">&lt;/%</span><span class="nb">def</span><span class="cp">&gt;</span><span class="x"></span>
</pre></div>
</div>
<p>To invoke the <tt class="docutils literal"><span class="pre">&lt;%def&gt;</span></tt>, it is normally called as an expression:</p>
<div class="highlight-mako"><div class="highlight"><pre><span class="x">the def:  </span><span class="cp">${</span><span class="n">hello</span><span class="p">()</span><span class="cp">}</span><span class="x"></span>
</pre></div>
</div>
<p>If the <tt class="docutils literal"><span class="pre">&lt;%def&gt;</span></tt> is not nested inside of another <tt class="docutils literal"><span class="pre">&lt;%def&gt;</span></tt>,
its known as a <strong>top level def</strong> and can be accessed anywhere in
the template, including above where it was defined.</p>
<p>All defs, top level or not, have access to the current
contextual namespace in exactly the same way their containing
template does. Suppose the template below is executed with the
variables <tt class="docutils literal"><span class="pre">username</span></tt> and <tt class="docutils literal"><span class="pre">accountdata</span></tt> inside the context:</p>
<div class="highlight-mako"><div class="highlight"><pre><span class="x">Hello there </span><span class="cp">${</span><span class="n">username</span><span class="cp">}</span><span class="x">, how are ya.  Lets see what your account says:</span>

<span class="cp">${</span><span class="n">account</span><span class="p">()</span><span class="cp">}</span><span class="x"></span>

<span class="cp">&lt;%</span><span class="nb">def</span> <span class="na">name=</span><span class="s">&quot;account()&quot;</span><span class="cp">&gt;</span><span class="x"></span>
<span class="x">    Account for </span><span class="cp">${</span><span class="n">username</span><span class="cp">}</span><span class="x">:&lt;br/&gt;</span>

    <span class="cp">%</span> <span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">accountdata</span><span class="p">:</span><span class="x"></span>
<span class="x">        Value: </span><span class="cp">${</span><span class="n">row</span><span class="cp">}</span><span class="x">&lt;br/&gt;</span>
    <span class="cp">%</span><span class="k"> endfor</span><span class="x"></span>
<span class="cp">&lt;/%</span><span class="nb">def</span><span class="cp">&gt;</span><span class="x"></span>
</pre></div>
</div>
<p>The <tt class="docutils literal"><span class="pre">username</span></tt> and <tt class="docutils literal"><span class="pre">accountdata</span></tt> variables are present
within the main template body as well as the body of the
<tt class="docutils literal"><span class="pre">account()</span></tt> def.</p>
<p>Since defs are just Python functions, you can define and pass
arguments to them as well:</p>
<div class="highlight-mako"><div class="highlight"><pre><span class="cp">${</span><span class="n">account</span><span class="p">(</span><span class="n">accountname</span><span class="o">=</span><span class="s">&#39;john&#39;</span><span class="p">)</span><span class="cp">}</span><span class="x"></span>

<span class="cp">&lt;%</span><span class="nb">def</span> <span class="na">name=</span><span class="s">&quot;account(accountname, type=&#39;regular&#39;)&quot;</span><span class="cp">&gt;</span><span class="x"></span>
<span class="x">    account name: </span><span class="cp">${</span><span class="n">accountname</span><span class="cp">}</span><span class="x">, type </span><span class="cp">${</span><span class="nb">type</span><span class="cp">}</span><span class="x"></span>
<span class="cp">&lt;/%</span><span class="nb">def</span><span class="cp">&gt;</span><span class="x"></span>
</pre></div>
</div>
<p>When you declare an argument signature for your def, they are
required to follow normal Python conventions (i.e., all
arguments are required except keyword arguments with a default
value). This is in contrast to using context-level variables,
which evaluate to <tt class="docutils literal"><span class="pre">UNDEFINED</span></tt> if you reference a name that
does not exist.</p>
<div class="section" id="calling-defs-from-other-files">
<h3>Calling defs from Other Files<a class="headerlink" href="#calling-defs-from-other-files" title="Permalink to this headline">¶</a></h3>
<p>Top level <tt class="docutils literal"><span class="pre">&lt;%defs&gt;</span></tt> are <strong>exported</strong> by your template&#8217;s
module, and can be called from the outside; including from other
templates, as well as normal Python code. Calling a <tt class="docutils literal"><span class="pre">&lt;%def&gt;</span></tt>
from another template is something like using an <tt class="docutils literal"><span class="pre">&lt;%include&gt;</span></tt>
- except you are calling a specific function within the
template, not the whole template.</p>
<p>The remote <tt class="docutils literal"><span class="pre">&lt;%def&gt;</span></tt> call is also a little bit like calling
functions from other modules in Python. There is an &#8220;import&#8221;
step to pull the names from another template into your own
template; then the function or functions are available.</p>
<p>To import another template, use the <tt class="docutils literal"><span class="pre">&lt;%namespace&gt;</span></tt> tag:</p>
<div class="highlight-mako"><div class="highlight"><pre><span class="cp">&lt;%</span><span class="nb">namespace</span> <span class="na">name=</span><span class="s">&quot;mystuff&quot;</span> <span class="na">file=</span><span class="s">&quot;mystuff.html&quot;</span><span class="cp">/&gt;</span><span class="x"></span>
</pre></div>
</div>
<p>The above tag adds a local variable &#8220;mystuff&#8221; to the current
scope.</p>
<p>Then, just call the defs off of <tt class="docutils literal"><span class="pre">mystuff</span></tt>:</p>
<div class="highlight-mako"><div class="highlight"><pre><span class="cp">${</span><span class="n">mystuff</span><span class="o">.</span><span class="n">somedef</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span><span class="n">y</span><span class="o">=</span><span class="mi">7</span><span class="p">)</span><span class="cp">}</span><span class="x"></span>
</pre></div>
</div>
<p>The <tt class="docutils literal"><span class="pre">&lt;%namespace&gt;</span></tt> tag also supports some of the other
semantics of Python&#8217;s <tt class="docutils literal"><span class="pre">import</span></tt> statement, including pulling
names into the local variable space, or using <tt class="docutils literal"><span class="pre">*</span></tt> to represent
all names, using the <tt class="docutils literal"><span class="pre">import</span></tt> attribute:</p>
<div class="highlight-mako"><div class="highlight"><pre><span class="cp">&lt;%</span><span class="nb">namespace</span> <span class="na">file=</span><span class="s">&quot;mystuff.html&quot;</span> <span class="na">import=</span><span class="s">&quot;foo, bar&quot;</span><span class="cp">/&gt;</span><span class="x"></span>
</pre></div>
</div>
<p>This is just a quick intro to the concept of a <strong>namespace</strong>,
which is a central Mako concept that has its own chapter in
these docs. For more detail and examples, see
<a class="reference internal" href="namespaces.html"><em>Namespaces</em></a>.</p>
</div>
<div class="section" id="calling-defs-programmatically">
<h3>Calling defs programmatically<a class="headerlink" href="#calling-defs-programmatically" title="Permalink to this headline">¶</a></h3>
<p>You can call def&#8217;s programmatically from any <a class="reference internal" href="usage.html#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a> object
using the <a class="reference internal" href="usage.html#mako.template.Template.get_def" title="mako.template.Template.get_def"><tt class="xref py py-meth docutils literal"><span class="pre">get_def()</span></tt></a> method, which returns a <a class="reference internal" href="usage.html#mako.template.DefTemplate" title="mako.template.DefTemplate"><tt class="xref py py-class docutils literal"><span class="pre">DefTemplate</span></tt></a>
object. This is a <a class="reference internal" href="usage.html#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a> subclass which the parent
<a class="reference internal" href="usage.html#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a> creates, and is usable like any other template:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">mako.template</span> <span class="kn">import</span> <span class="n">Template</span>

<span class="n">template</span> <span class="o">=</span> <span class="n">Template</span><span class="p">(</span><span class="s">&quot;&quot;&quot;</span>
<span class="s">    &lt;</span><span class="si">%d</span><span class="s">ef name=&quot;hi(name)&quot;&gt;</span>
<span class="s">        hi ${name}!</span>
<span class="s">    &lt;/</span><span class="si">%d</span><span class="s">ef&gt;</span>

<span class="s">    &lt;</span><span class="si">%d</span><span class="s">ef name=&quot;bye(name)&quot;&gt;</span>
<span class="s">        bye ${name}!</span>
<span class="s">    &lt;/</span><span class="si">%d</span><span class="s">ef&gt;</span>
<span class="s">&quot;&quot;&quot;</span><span class="p">)</span>

<span class="k">print</span> <span class="n">template</span><span class="o">.</span><span class="n">get_def</span><span class="p">(</span><span class="s">&quot;hi&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">render</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">&quot;ed&quot;</span><span class="p">)</span>
<span class="k">print</span> <span class="n">template</span><span class="o">.</span><span class="n">get_def</span><span class="p">(</span><span class="s">&quot;bye&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">render</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">&quot;ed&quot;</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="defs-within-defs">
<h3>Defs within Defs<a class="headerlink" href="#defs-within-defs" title="Permalink to this headline">¶</a></h3>
<p>The def model follows regular Python rules for closures.
Declaring <tt class="docutils literal"><span class="pre">&lt;%def&gt;</span></tt> inside another <tt class="docutils literal"><span class="pre">&lt;%def&gt;</span></tt> declares it
within the parent&#8217;s <strong>enclosing scope</strong>:</p>
<div class="highlight-mako"><div class="highlight"><pre><span class="cp">&lt;%</span><span class="nb">def</span> <span class="na">name=</span><span class="s">&quot;mydef()&quot;</span><span class="cp">&gt;</span><span class="x"></span>
<span class="x">    </span><span class="cp">&lt;%</span><span class="nb">def</span> <span class="na">name=</span><span class="s">&quot;subdef()&quot;</span><span class="cp">&gt;</span><span class="x"></span>
<span class="x">        a sub def</span>
<span class="x">    </span><span class="cp">&lt;/%</span><span class="nb">def</span><span class="cp">&gt;</span><span class="x"></span>

<span class="x">    im the def, and the subcomponent is </span><span class="cp">${</span><span class="n">subdef</span><span class="p">()</span><span class="cp">}</span><span class="x"></span>
<span class="cp">&lt;/%</span><span class="nb">def</span><span class="cp">&gt;</span><span class="x"></span>
</pre></div>
</div>
<p>Just like Python, names that exist outside the inner <tt class="docutils literal"><span class="pre">&lt;%def&gt;</span></tt>
exist inside it as well:</p>
<div class="highlight-mako"><div class="highlight"><pre><span class="cp">&lt;%</span>
    <span class="n">x</span> <span class="o">=</span> <span class="mi">12</span>
<span class="cp">%&gt;</span><span class="x"></span>
<span class="cp">&lt;%</span><span class="nb">def</span> <span class="na">name=</span><span class="s">&quot;outer()&quot;</span><span class="cp">&gt;</span><span class="x"></span>
<span class="x">    </span><span class="cp">&lt;%</span>
        <span class="n">y</span> <span class="o">=</span> <span class="mi">15</span>
    <span class="cp">%&gt;</span><span class="x"></span>
<span class="x">    </span><span class="cp">&lt;%</span><span class="nb">def</span> <span class="na">name=</span><span class="s">&quot;inner()&quot;</span><span class="cp">&gt;</span><span class="x"></span>
<span class="x">        inner, x is </span><span class="cp">${</span><span class="n">x</span><span class="cp">}</span><span class="x">, y is </span><span class="cp">${</span><span class="n">y</span><span class="cp">}</span><span class="x"></span>
<span class="x">    </span><span class="cp">&lt;/%</span><span class="nb">def</span><span class="cp">&gt;</span><span class="x"></span>

<span class="x">    outer, x is </span><span class="cp">${</span><span class="n">x</span><span class="cp">}</span><span class="x">, y is </span><span class="cp">${</span><span class="n">y</span><span class="cp">}</span><span class="x"></span>
<span class="cp">&lt;/%</span><span class="nb">def</span><span class="cp">&gt;</span><span class="x"></span>
</pre></div>
</div>
<p>Assigning to a name inside of a def declares that name as local
to the scope of that def (again, like Python itself). This means
the following code will raise an error:</p>
<div class="highlight-mako"><div class="highlight"><pre><span class="cp">&lt;%</span>
    <span class="n">x</span> <span class="o">=</span> <span class="mi">10</span>
<span class="cp">%&gt;</span><span class="x"></span>
<span class="cp">&lt;%</span><span class="nb">def</span> <span class="na">name=</span><span class="s">&quot;somedef()&quot;</span><span class="cp">&gt;</span>
    <span class="cp">## error !</span><span class="x"></span>
<span class="x">    somedef, x is </span><span class="cp">${</span><span class="n">x</span><span class="cp">}</span><span class="x"></span>
<span class="x">    </span><span class="cp">&lt;%</span>
        <span class="n">x</span> <span class="o">=</span> <span class="mi">27</span>
    <span class="cp">%&gt;</span><span class="x"></span>
<span class="cp">&lt;/%</span><span class="nb">def</span><span class="cp">&gt;</span><span class="x"></span>
</pre></div>
</div>
<p>...because the assignment to <tt class="docutils literal"><span class="pre">x</span></tt> declares x as local to the
scope of <tt class="docutils literal"><span class="pre">somedef</span></tt>, rendering the &#8220;outer&#8221; version unreachable
in the expression that tries to render it.</p>
</div>
<div class="section" id="calling-a-def-with-embedded-content-and-or-other-defs">
<span id="defs-with-content"></span><h3>Calling a def with embedded content and/or other defs<a class="headerlink" href="#calling-a-def-with-embedded-content-and-or-other-defs" title="Permalink to this headline">¶</a></h3>
<p>A flip-side to def within def is a def call with content. This
is where you call a def, and at the same time declare a block of
content (or multiple blocks) that can be used by the def being
called. The main point of such a call is to create custom,
nestable tags, just like any other template language&#8217;s
custom-tag creation system - where the external tag controls the
execution of the nested tags and can communicate state to them.
Only with Mako, you don&#8217;t have to use any external Python
modules, you can define arbitrarily nestable tags right in your
templates.</p>
<p>To achieve this, the target def is invoked using the form
<tt class="docutils literal"><span class="pre">&lt;%namepacename:defname&gt;</span></tt> instead of the normal <tt class="docutils literal"><span class="pre">${}</span></tt>
syntax. This syntax, introduced in Mako 0.2.3, is functionally
equivalent another tag known as <tt class="docutils literal"><span class="pre">call</span></tt>, which takes the form
<tt class="docutils literal"><span class="pre">&lt;%call</span> <span class="pre">expr='namespacename.defname(args)'&gt;</span></tt>. While <tt class="docutils literal"><span class="pre">%call</span></tt>
is available in all versions of Mako, the newer style is
probably more familiar looking. The <tt class="docutils literal"><span class="pre">namespace</span></tt> portion of the
call is the name of the <strong>namespace</strong> in which the def is
defined - in the most simple cases, this can be <tt class="docutils literal"><span class="pre">local</span></tt> or
<tt class="docutils literal"><span class="pre">self</span></tt> to reference the current template&#8217;s namespace (the
difference between <tt class="docutils literal"><span class="pre">local</span></tt> and <tt class="docutils literal"><span class="pre">self</span></tt> is one of inheritance
- see <a class="reference internal" href="namespaces.html#namespaces-builtin"><em>Built-in Namespaces</em></a> for details).</p>
<p>When the target def is invoked, a variable <tt class="docutils literal"><span class="pre">caller</span></tt> is placed
in its context which contains another namespace containing the
body and other defs defined by the caller. The body itself is
referenced by the method <tt class="docutils literal"><span class="pre">body()</span></tt>. Below, we build a <tt class="docutils literal"><span class="pre">%def</span></tt>
that operates upon <tt class="docutils literal"><span class="pre">caller.body()</span></tt> to invoke the body of the
custom tag:</p>
<div class="highlight-mako"><div class="highlight"><pre><span class="cp">&lt;%</span><span class="nb">def</span> <span class="na">name=</span><span class="s">&quot;buildtable()&quot;</span><span class="cp">&gt;</span><span class="x"></span>
<span class="x">    &lt;table&gt;</span>
<span class="x">        &lt;tr&gt;&lt;td&gt;</span>
<span class="x">            </span><span class="cp">${</span><span class="n">caller</span><span class="o">.</span><span class="n">body</span><span class="p">()</span><span class="cp">}</span><span class="x"></span>
<span class="x">        &lt;/td&gt;&lt;/tr&gt;</span>
<span class="x">    &lt;/table&gt;</span>
<span class="cp">&lt;/%</span><span class="nb">def</span><span class="cp">&gt;</span><span class="x"></span>

<span class="cp">&lt;%</span><span class="nb">self:buildtable</span><span class="cp">&gt;</span><span class="x"></span>
<span class="x">    I am the table body.</span>
<span class="cp">&lt;/%</span><span class="nb">self:buildtable</span><span class="cp">&gt;</span><span class="x"></span>
</pre></div>
</div>
<p>This produces the output (whitespace formatted):</p>
<div class="highlight-html"><div class="highlight"><pre><span class="nt">&lt;table&gt;</span>
    <span class="nt">&lt;tr&gt;&lt;td&gt;</span>
        I am the table body.
    <span class="nt">&lt;/td&gt;&lt;/tr&gt;</span>
<span class="nt">&lt;/table&gt;</span>
</pre></div>
</div>
<p>Using the older <tt class="docutils literal"><span class="pre">%call</span></tt> syntax looks like:</p>
<div class="highlight-mako"><div class="highlight"><pre><span class="cp">&lt;%</span><span class="nb">def</span> <span class="na">name=</span><span class="s">&quot;buildtable()&quot;</span><span class="cp">&gt;</span><span class="x"></span>
<span class="x">    &lt;table&gt;</span>
<span class="x">        &lt;tr&gt;&lt;td&gt;</span>
<span class="x">            </span><span class="cp">${</span><span class="n">caller</span><span class="o">.</span><span class="n">body</span><span class="p">()</span><span class="cp">}</span><span class="x"></span>
<span class="x">        &lt;/td&gt;&lt;/tr&gt;</span>
<span class="x">    &lt;/table&gt;</span>
<span class="cp">&lt;/%</span><span class="nb">def</span><span class="cp">&gt;</span><span class="x"></span>

<span class="cp">&lt;%</span><span class="nb">call</span> <span class="na">expr=</span><span class="s">&quot;buildtable()&quot;</span><span class="cp">&gt;</span><span class="x"></span>
<span class="x">    I am the table body.</span>
<span class="cp">&lt;/%</span><span class="nb">call</span><span class="cp">&gt;</span><span class="x"></span>
</pre></div>
</div>
<p>The <tt class="docutils literal"><span class="pre">body()</span></tt> can be executed multiple times or not at all.
This means you can use def-call-with-content to build iterators,
conditionals, etc:</p>
<div class="highlight-mako"><div class="highlight"><pre><span class="cp">&lt;%</span><span class="nb">def</span> <span class="na">name=</span><span class="s">&quot;lister(count)&quot;</span><span class="cp">&gt;</span>
    <span class="cp">%</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">count</span><span class="p">):</span><span class="x"></span>
<span class="x">        </span><span class="cp">${</span><span class="n">caller</span><span class="o">.</span><span class="n">body</span><span class="p">()</span><span class="cp">}</span>
    <span class="cp">%</span><span class="k"> endfor</span><span class="x"></span>
<span class="cp">&lt;/%</span><span class="nb">def</span><span class="cp">&gt;</span><span class="x"></span>

<span class="cp">&lt;%</span><span class="nb">self:lister</span> <span class="na">count=</span><span class="s">&quot;${3}&quot;</span><span class="cp">&gt;</span><span class="x"></span>
<span class="x">    hi</span>
<span class="cp">&lt;/%</span><span class="nb">self:lister</span><span class="cp">&gt;</span><span class="x"></span>
</pre></div>
</div>
<p>Produces:</p>
<div class="highlight-html"><div class="highlight"><pre>hi
hi
hi
</pre></div>
</div>
<p>Notice above we pass <tt class="docutils literal"><span class="pre">3</span></tt> as a Python expression, so that it
remains as an integer.</p>
<p>A custom &#8220;conditional&#8221; tag:</p>
<div class="highlight-mako"><div class="highlight"><pre><span class="cp">&lt;%</span><span class="nb">def</span> <span class="na">name=</span><span class="s">&quot;conditional(expression)&quot;</span><span class="cp">&gt;</span>
    <span class="cp">%</span> <span class="k">if</span> <span class="n">expression</span><span class="p">:</span><span class="x"></span>
<span class="x">        </span><span class="cp">${</span><span class="n">caller</span><span class="o">.</span><span class="n">body</span><span class="p">()</span><span class="cp">}</span>
    <span class="cp">%</span><span class="k"> endif</span><span class="x"></span>
<span class="cp">&lt;/%</span><span class="nb">def</span><span class="cp">&gt;</span><span class="x"></span>

<span class="cp">&lt;%</span><span class="nb">self:conditional</span> <span class="na">expression=</span><span class="s">&quot;${4==4}&quot;</span><span class="cp">&gt;</span><span class="x"></span>
<span class="x">    im the result</span>
<span class="cp">&lt;/%</span><span class="nb">self:conditional</span><span class="cp">&gt;</span><span class="x"></span>
</pre></div>
</div>
<p>Produces:</p>
<div class="highlight-html"><div class="highlight"><pre>im the result
</pre></div>
</div>
<p>But that&#8217;s not all. The <tt class="docutils literal"><span class="pre">body()</span></tt> function also can handle
arguments, which will augment the local namespace of the body
callable. The caller must define the arguments which it expects
to receive from its target def using the <tt class="docutils literal"><span class="pre">args</span></tt> attribute,
which is a comma-separated list of argument names. Below, our
<tt class="docutils literal"><span class="pre">&lt;%def&gt;</span></tt> calls the <tt class="docutils literal"><span class="pre">body()</span></tt> of its caller, passing in an
element of data from its argument:</p>
<div class="highlight-mako"><div class="highlight"><pre><span class="cp">&lt;%</span><span class="nb">def</span> <span class="na">name=</span><span class="s">&quot;layoutdata(somedata)&quot;</span><span class="cp">&gt;</span><span class="x"></span>
<span class="x">    &lt;table&gt;</span>
    <span class="cp">%</span> <span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="n">somedata</span><span class="p">:</span><span class="x"></span>
<span class="x">        &lt;tr&gt;</span>
        <span class="cp">%</span> <span class="k">for</span> <span class="n">col</span> <span class="ow">in</span> <span class="n">item</span><span class="p">:</span><span class="x"></span>
<span class="x">            &lt;td&gt;</span><span class="cp">${</span><span class="n">caller</span><span class="o">.</span><span class="n">body</span><span class="p">(</span><span class="n">col</span><span class="o">=</span><span class="n">col</span><span class="p">)</span><span class="cp">}</span><span class="x">&lt;/td&gt;</span>
        <span class="cp">%</span><span class="k"> endfor</span><span class="x"></span>
<span class="x">        &lt;/tr&gt;</span>
    <span class="cp">%</span><span class="k"> endfor</span><span class="x"></span>
<span class="x">    &lt;/table&gt;</span>
<span class="cp">&lt;/%</span><span class="nb">def</span><span class="cp">&gt;</span><span class="x"></span>

<span class="cp">&lt;%</span><span class="nb">self:layoutdata</span> <span class="na">somedata=</span><span class="s">&quot;${[[1,2,3],[4,5,6],[7,8,9]]}&quot;</span> <span class="na">args=</span><span class="s">&quot;col&quot;</span><span class="cp">&gt;</span><span class="x">\</span>
<span class="x">Body data: </span><span class="cp">${</span><span class="n">col</span><span class="cp">}</span><span class="x">\</span>
<span class="cp">&lt;/%</span><span class="nb">self:layoutdata</span><span class="cp">&gt;</span><span class="x"></span>
</pre></div>
</div>
<p>Produces:</p>
<div class="highlight-html"><div class="highlight"><pre><span class="nt">&lt;table&gt;</span>
   <span class="nt">&lt;tr&gt;</span>
       <span class="nt">&lt;td&gt;</span>Body data: 1<span class="nt">&lt;/td&gt;</span>
       <span class="nt">&lt;td&gt;</span>Body data: 2<span class="nt">&lt;/td&gt;</span>
       <span class="nt">&lt;td&gt;</span>Body data: 3<span class="nt">&lt;/td&gt;</span>
   <span class="nt">&lt;/tr&gt;</span>
   <span class="nt">&lt;tr&gt;</span>
       <span class="nt">&lt;td&gt;</span>Body data: 4<span class="nt">&lt;/td&gt;</span>
       <span class="nt">&lt;td&gt;</span>Body data: 5<span class="nt">&lt;/td&gt;</span>
       <span class="nt">&lt;td&gt;</span>Body data: 6<span class="nt">&lt;/td&gt;</span>
   <span class="nt">&lt;/tr&gt;</span>
   <span class="nt">&lt;tr&gt;</span>
       <span class="nt">&lt;td&gt;</span>Body data: 7<span class="nt">&lt;/td&gt;</span>
       <span class="nt">&lt;td&gt;</span>Body data: 8<span class="nt">&lt;/td&gt;</span>
       <span class="nt">&lt;td&gt;</span>Body data: 9<span class="nt">&lt;/td&gt;</span>
   <span class="nt">&lt;/tr&gt;</span>
<span class="nt">&lt;/table&gt;</span>
</pre></div>
</div>
<p>You don&#8217;t have to stick to calling just the <tt class="docutils literal"><span class="pre">body()</span></tt> function.
The caller can define any number of callables, allowing the
<tt class="docutils literal"><span class="pre">&lt;%call&gt;</span></tt> tag to produce whole layouts:</p>
<div class="highlight-mako"><div class="highlight"><pre><span class="cp">&lt;%</span><span class="nb">def</span> <span class="na">name=</span><span class="s">&quot;layout()&quot;</span><span class="cp">&gt;</span>
    <span class="cp">## a layout def</span><span class="x"></span>
<span class="x">    &lt;div class=&quot;mainlayout&quot;&gt;</span>
<span class="x">        &lt;div class=&quot;header&quot;&gt;</span>
<span class="x">            </span><span class="cp">${</span><span class="n">caller</span><span class="o">.</span><span class="n">header</span><span class="p">()</span><span class="cp">}</span><span class="x"></span>
<span class="x">        &lt;/div&gt;</span>
<span class="x">        &lt;div class=&quot;sidebar&quot;&gt;</span>
<span class="x">            </span><span class="cp">${</span><span class="n">caller</span><span class="o">.</span><span class="n">sidebar</span><span class="p">()</span><span class="cp">}</span><span class="x"></span>
<span class="x">        &lt;/div&gt;</span>
<span class="x">        &lt;div class=&quot;content&quot;&gt;</span>
<span class="x">            </span><span class="cp">${</span><span class="n">caller</span><span class="o">.</span><span class="n">body</span><span class="p">()</span><span class="cp">}</span><span class="x"></span>
<span class="x">        &lt;/div&gt;</span>
<span class="x">    &lt;/div&gt;</span>
<span class="cp">&lt;/%</span><span class="nb">def</span><span class="cp">&gt;</span>

<span class="cp">## calls the layout def</span><span class="x"></span>
<span class="cp">&lt;%</span><span class="nb">self:layout</span><span class="cp">&gt;</span><span class="x"></span>
<span class="x">    </span><span class="cp">&lt;%</span><span class="nb">def</span> <span class="na">name=</span><span class="s">&quot;header()&quot;</span><span class="cp">&gt;</span><span class="x"></span>
<span class="x">        I am the header</span>
<span class="x">    </span><span class="cp">&lt;/%</span><span class="nb">def</span><span class="cp">&gt;</span><span class="x"></span>
<span class="x">    </span><span class="cp">&lt;%</span><span class="nb">def</span> <span class="na">name=</span><span class="s">&quot;sidebar()&quot;</span><span class="cp">&gt;</span><span class="x"></span>
<span class="x">        &lt;ul&gt;</span>
<span class="x">            &lt;li&gt;sidebar 1&lt;/li&gt;</span>
<span class="x">            &lt;li&gt;sidebar 2&lt;/li&gt;</span>
<span class="x">        &lt;/ul&gt;</span>
<span class="x">    </span><span class="cp">&lt;/%</span><span class="nb">def</span><span class="cp">&gt;</span><span class="x"></span>

<span class="x">        this is the body</span>
<span class="cp">&lt;/%</span><span class="nb">self:layout</span><span class="cp">&gt;</span><span class="x"></span>
</pre></div>
</div>
<p>The above layout would produce:</p>
<div class="highlight-html"><div class="highlight"><pre><span class="nt">&lt;div</span> <span class="na">class=</span><span class="s">&quot;mainlayout&quot;</span><span class="nt">&gt;</span>
   <span class="nt">&lt;div</span> <span class="na">class=</span><span class="s">&quot;header&quot;</span><span class="nt">&gt;</span>
   I am the header
   <span class="nt">&lt;/div&gt;</span>

   <span class="nt">&lt;div</span> <span class="na">class=</span><span class="s">&quot;sidebar&quot;</span><span class="nt">&gt;</span>
   <span class="nt">&lt;ul&gt;</span>
       <span class="nt">&lt;li&gt;</span>sidebar 1<span class="nt">&lt;/li&gt;</span>
       <span class="nt">&lt;li&gt;</span>sidebar 2<span class="nt">&lt;/li&gt;</span>
   <span class="nt">&lt;/ul&gt;</span>
   <span class="nt">&lt;/div&gt;</span>

   <span class="nt">&lt;div</span> <span class="na">class=</span><span class="s">&quot;content&quot;</span><span class="nt">&gt;</span>
   this is the body
   <span class="nt">&lt;/div&gt;</span>
<span class="nt">&lt;/div&gt;</span>
</pre></div>
</div>
<p>The number of things you can do with <tt class="docutils literal"><span class="pre">&lt;%call&gt;</span></tt> and/or the
<tt class="docutils literal"><span class="pre">&lt;%namespacename:defname&gt;</span></tt> calling syntax is enormous. You can
create form widget libraries, such as an enclosing <tt class="docutils literal"><span class="pre">&lt;FORM&gt;</span></tt>
tag and nested HTML input elements, or portable wrapping schemes
using <tt class="docutils literal"><span class="pre">&lt;div&gt;</span></tt> or other elements. You can create tags that
interpret rows of data, such as from a database, providing the
individual columns of each row to a <tt class="docutils literal"><span class="pre">body()</span></tt> callable which
lays out the row any way it wants. Basically anything you&#8217;d do
with a &#8220;custom tag&#8221; or tag library in some other system, Mako
provides via <tt class="docutils literal"><span class="pre">&lt;%def&gt;</span></tt> tags and plain Python callables which are
invoked via <tt class="docutils literal"><span class="pre">&lt;%namespacename:defname&gt;</span></tt> or <tt class="docutils literal"><span class="pre">&lt;%call&gt;</span></tt>.</p>
</div>
</div>
<div class="section" id="using-blocks">
<span id="blocks"></span><h2>Using Blocks<a class="headerlink" href="#using-blocks" title="Permalink to this headline">¶</a></h2>
<p>The <tt class="docutils literal"><span class="pre">&lt;%block&gt;</span></tt> tag is new as of Mako 0.4.1, and introduces some new twists on the
<tt class="docutils literal"><span class="pre">&lt;%def&gt;</span></tt> tag which make it more closely tailored towards layout.</p>
<p>An example of a block:</p>
<div class="highlight-mako"><div class="highlight"><pre><span class="x">&lt;html&gt;</span>
<span class="x">    &lt;body&gt;</span>
<span class="x">        </span><span class="cp">&lt;%</span><span class="nb">block</span><span class="cp">&gt;</span><span class="x"></span>
<span class="x">            this is a block.</span>
<span class="x">        </span><span class="cp">&lt;/%</span><span class="nb">block</span><span class="cp">&gt;</span><span class="x"></span>
<span class="x">    &lt;/body&gt;</span>
<span class="x">&lt;/html&gt;</span>
</pre></div>
</div>
<p>In the above example, we define a simple block.  The block renders its content in the place
that it&#8217;s defined.  Since the block is called for us, it doesn&#8217;t need a name and the above
is referred to as an <strong>anonymous block</strong>.  So the output of the above template will be:</p>
<div class="highlight-html"><div class="highlight"><pre><span class="nt">&lt;html&gt;</span>
    <span class="nt">&lt;body&gt;</span>
            this is a block.
    <span class="nt">&lt;/body&gt;</span>
<span class="nt">&lt;/html&gt;</span>
</pre></div>
</div>
<p>So in fact the above block has absolutely no effect.   Its usefulness comes when we start
using modifiers.  Such as, we can apply a filter to our block:</p>
<div class="highlight-mako"><div class="highlight"><pre><span class="x">&lt;html&gt;</span>
<span class="x">    &lt;body&gt;</span>
<span class="x">        </span><span class="cp">&lt;%</span><span class="nb">block</span> <span class="na">filter=</span><span class="s">&quot;h&quot;</span><span class="cp">&gt;</span><span class="x"></span>
<span class="x">            &lt;html&gt;this is some escaped html.&lt;/html&gt;</span>
<span class="x">        </span><span class="cp">&lt;/%</span><span class="nb">block</span><span class="cp">&gt;</span><span class="x"></span>
<span class="x">    &lt;/body&gt;</span>
<span class="x">&lt;/html&gt;</span>
</pre></div>
</div>
<p>or perhaps a caching directive:</p>
<div class="highlight-mako"><div class="highlight"><pre><span class="x">&lt;html&gt;</span>
<span class="x">    &lt;body&gt;</span>
<span class="x">        </span><span class="cp">&lt;%</span><span class="nb">block</span> <span class="na">cached=</span><span class="s">&quot;True&quot;</span> <span class="na">cache_timeout=</span><span class="s">&quot;60&quot;</span><span class="cp">&gt;</span><span class="x"></span>
<span class="x">            This content will be cached for 60 seconds.</span>
<span class="x">        </span><span class="cp">&lt;/%</span><span class="nb">block</span><span class="cp">&gt;</span><span class="x"></span>
<span class="x">    &lt;/body&gt;</span>
<span class="x">&lt;/html&gt;</span>
</pre></div>
</div>
<p>Blocks also work in iterations, conditionals, just like defs:</p>
<div class="highlight-mako"><div class="highlight"><pre><span class="cp">%</span> <span class="k">if</span> <span class="n">some_condition</span><span class="p">:</span><span class="x"></span>
<span class="x">    </span><span class="cp">&lt;%</span><span class="nb">block</span><span class="cp">&gt;</span><span class="x">condition is met</span><span class="cp">&lt;/%</span><span class="nb">block</span><span class="cp">&gt;</span>
<span class="cp">%</span><span class="k"> endif</span><span class="x"></span>
</pre></div>
</div>
<p>While the block renders at the point it is defined in the template,
the underlying function is present in the generated Python code only
once, so there&#8217;s no issue with placing a block inside of a loop or
similar. Anonymous blocks are defined as closures in the local
rendering body, so have access to local variable scope:</p>
<div class="highlight-mako"><div class="highlight"><pre><span class="cp">%</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">4</span><span class="p">):</span><span class="x"></span>
<span class="x">    </span><span class="cp">&lt;%</span><span class="nb">block</span><span class="cp">&gt;</span><span class="x">i is </span><span class="cp">${</span><span class="n">i</span><span class="cp">}&lt;/%</span><span class="nb">block</span><span class="cp">&gt;</span>
<span class="cp">%</span><span class="k"> endfor</span><span class="x"></span>
</pre></div>
</div>
<div class="section" id="using-named-blocks">
<h3>Using Named Blocks<a class="headerlink" href="#using-named-blocks" title="Permalink to this headline">¶</a></h3>
<p>Possibly the more important area where blocks are useful is when we
do actually give them names. Named blocks are tailored to behave
somewhat closely to Jinja2&#8217;s block tag, in that they define an area
of a layout which can be overridden by an inheriting template. In
sharp contrast to the <tt class="docutils literal"><span class="pre">&lt;%def&gt;</span></tt> tag, the name given to a block is
global for the entire template regardless of how deeply it&#8217;s nested:</p>
<div class="highlight-mako"><div class="highlight"><pre><span class="x">&lt;html&gt;</span>
<span class="cp">&lt;%</span><span class="nb">block</span> <span class="na">name=</span><span class="s">&quot;header&quot;</span><span class="cp">&gt;</span><span class="x"></span>
<span class="x">    &lt;head&gt;</span>
<span class="x">        &lt;title&gt;</span>
<span class="x">            </span><span class="cp">&lt;%</span><span class="nb">block</span> <span class="na">name=</span><span class="s">&quot;title&quot;</span><span class="cp">&gt;</span><span class="x">Title</span><span class="cp">&lt;/%</span><span class="nb">block</span><span class="cp">&gt;</span><span class="x"></span>
<span class="x">        &lt;/title&gt;</span>
<span class="x">    &lt;/head&gt;</span>
<span class="cp">&lt;/%</span><span class="nb">block</span><span class="cp">&gt;</span><span class="x"></span>
<span class="x">&lt;body&gt;</span>
<span class="x">    </span><span class="cp">${</span><span class="nb">next</span><span class="o">.</span><span class="n">body</span><span class="p">()</span><span class="cp">}</span><span class="x"></span>
<span class="x">&lt;/body&gt;</span>
<span class="x">&lt;/html&gt;</span>
</pre></div>
</div>
<p>The above example has two named blocks &#8220;<tt class="docutils literal"><span class="pre">header</span></tt>&#8221; and &#8220;<tt class="docutils literal"><span class="pre">title</span></tt>&#8220;, both of which can be referred to
by an inheriting template.  A detailed walkthrough of this usage can be found at <a class="reference internal" href="inheritance.html"><em>Inheritance</em></a>.</p>
<p>Note above that named blocks don&#8217;t have any argument declaration the way defs do.   They still implement themselves
as Python functions, however, so they can be invoked additional times beyond their initial definition:</p>
<div class="highlight-mako"><div class="highlight"><pre><span class="x">&lt;div name=&quot;page&quot;&gt;</span>
<span class="x">    </span><span class="cp">&lt;%</span><span class="nb">block</span> <span class="na">name=</span><span class="s">&quot;pagecontrol&quot;</span><span class="cp">&gt;</span><span class="x"></span>
<span class="x">        &lt;a href=&quot;&quot;&gt;previous page&lt;/a&gt; |</span>
<span class="x">        &lt;a href=&quot;&quot;&gt;next page&lt;/a&gt;</span>
<span class="x">    </span><span class="cp">&lt;/%</span><span class="nb">block</span><span class="cp">&gt;</span><span class="x"></span>

<span class="x">    &lt;table&gt;</span>
<span class="x">        ## some content</span>
<span class="x">    &lt;/table&gt;</span>

<span class="x">    </span><span class="cp">${</span><span class="n">pagecontrol</span><span class="p">()</span><span class="cp">}</span><span class="x"></span>
<span class="x">&lt;/div&gt;</span>
</pre></div>
</div>
<p>The content referenced by <tt class="docutils literal"><span class="pre">pagecontrol</span></tt> above will be rendered both above and below the <tt class="docutils literal"><span class="pre">&lt;table&gt;</span></tt> tags.</p>
<p>To keep things sane, named blocks have restrictions that defs do not:</p>
<ul class="simple">
<li>The <tt class="docutils literal"><span class="pre">&lt;%block&gt;</span></tt> declaration cannot have any argument signature.</li>
<li>The name of a <tt class="docutils literal"><span class="pre">&lt;%block&gt;</span></tt> can only be defined once in a template - an error is raised if two blocks of the same
name occur anywhere in a single template, regardless of nesting.  A similar error is raised if a top level def
shares the same name as that of a block.</li>
<li>A named <tt class="docutils literal"><span class="pre">&lt;%block&gt;</span></tt> cannot be defined within a <tt class="docutils literal"><span class="pre">&lt;%def&gt;</span></tt>, or inside the body of a &#8220;call&#8221;, i.e.
<tt class="docutils literal"><span class="pre">&lt;%call&gt;</span></tt> or <tt class="docutils literal"><span class="pre">&lt;%namespacename:defname&gt;</span></tt> tag.   Anonymous blocks can, however.</li>
</ul>
</div>
<div class="section" id="using-page-arguments-in-named-blocks">
<h3>Using page arguments in named blocks<a class="headerlink" href="#using-page-arguments-in-named-blocks" title="Permalink to this headline">¶</a></h3>
<p>A named block is very much like a top level def. It has a similar
restriction to these types of defs in that arguments passed to the
template via the <tt class="docutils literal"><span class="pre">&lt;%page&gt;</span></tt> tag aren&#8217;t automatically available.
Using arguments with the <tt class="docutils literal"><span class="pre">&lt;%page&gt;</span></tt> tag is described in the section
<a class="reference internal" href="namespaces.html#namespaces-body"><em>The &#8220;body()&#8221; method</em></a>, and refers to scenarios such as when the
<tt class="docutils literal"><span class="pre">body()</span></tt> method of a template is called from an inherited template passing
arguments, or the template is invoked from an <tt class="docutils literal"><span class="pre">&lt;%include&gt;</span></tt> tag
with arguments. To allow a named block to share the same arguments
passed to the page, the <tt class="docutils literal"><span class="pre">args</span></tt> attribute can be used:</p>
<div class="highlight-mako"><div class="highlight"><pre><span class="cp">&lt;%</span><span class="nb">page</span> <span class="na">args=</span><span class="s">&quot;post&quot;</span><span class="cp">/&gt;</span><span class="x"></span>

<span class="x">&lt;a name=&quot;</span><span class="cp">${</span><span class="n">post</span><span class="o">.</span><span class="n">title</span><span class="cp">}</span><span class="x">&quot; /&gt;</span>

<span class="x">&lt;span class=&quot;post_prose&quot;&gt;</span>
<span class="x">    </span><span class="cp">&lt;%</span><span class="nb">block</span> <span class="na">name=</span><span class="s">&quot;post_prose&quot;</span> <span class="na">args=</span><span class="s">&quot;post&quot;</span><span class="cp">&gt;</span><span class="x"></span>
<span class="x">        </span><span class="cp">${</span><span class="n">post</span><span class="o">.</span><span class="n">content</span><span class="cp">}</span><span class="x"></span>
<span class="x">    </span><span class="cp">&lt;/%</span><span class="nb">block</span><span class="cp">&gt;</span><span class="x"></span>
<span class="x">&lt;/span&gt;</span>
</pre></div>
</div>
<p>Where above, if the template is called via a directive like
<tt class="docutils literal"><span class="pre">&lt;%include</span> <span class="pre">file=&quot;post.mako&quot;</span> <span class="pre">args=&quot;post=post&quot;</span> <span class="pre">/&gt;</span></tt>, the <tt class="docutils literal"><span class="pre">post</span></tt>
variable is available both in the main body as well as the
<tt class="docutils literal"><span class="pre">post_prose</span></tt> block.</p>
<p>Similarly, the <tt class="docutils literal"><span class="pre">**pageargs</span></tt> variable is present, in named blocks only,
for those arguments not explicit in the <tt class="docutils literal"><span class="pre">&lt;%page&gt;</span></tt> tag:</p>
<div class="highlight-mako"><div class="highlight"><pre><span class="cp">&lt;%</span><span class="nb">block</span> <span class="na">name=</span><span class="s">&quot;post_prose&quot;</span><span class="cp">&gt;</span><span class="x"></span>
<span class="x">    </span><span class="cp">${</span><span class="n">pageargs</span><span class="p">[</span><span class="s">&#39;post&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">content</span><span class="cp">}</span><span class="x"></span>
<span class="cp">&lt;/%</span><span class="nb">block</span><span class="cp">&gt;</span><span class="x"></span>
</pre></div>
</div>
<p>The <tt class="docutils literal"><span class="pre">args</span></tt> attribute is only allowed with named blocks. With
anonymous blocks, the Python function is always rendered in the same
scope as the call itself, so anything available directly outside the
anonymous block is available inside as well.</p>
</div>
</div>
</div>

            </div>
        </div>

        
            <div class="bottomnav">
                
<div class="prevnext">
        Previous:
        <a href="syntax.html" title="previous chapter">Syntax</a>
        Next:
        <a href="runtime.html" title="next chapter">The Mako Runtime Environment</a>
</div>

                <div class="doc_copyright">
                    &copy; Copyright the Mako authors and contributors.
                    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.1.
                </div>
            </div>
        





    </body>
</html>



